package com.yst.gateway.monitor.services.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yst.gateway.monitor.common.utils.DateUtils;
import com.yst.gateway.monitor.common.utils.LoginUserCache;
import com.yst.gateway.monitor.common.utils.UUIDGenerator;
import com.yst.gateway.monitor.dao.SysRolesMapper;
import com.yst.gateway.monitor.models.SysRoles;
import com.yst.gateway.monitor.services.SysRoleService;


@Service
@Scope("prototype")
public class SysRoleServiceImpl implements SysRoleService {
	
	@Autowired
	private SysRolesMapper sysRolesMapper;
	
	@Override
	public PageInfo<SysRoles> getByPage(int pageNum, int pageSize,
			Map<String, Object> paramMap) {
		PageHelper.startPage(pageNum, pageSize);
		return new PageInfo<SysRoles>(sysRolesMapper.selectAll(paramMap));
	}

	@Override
	public boolean add(SysRoles t) {
		boolean flag = true;
		t.setSysRoleId(UUIDGenerator.getUUID());
		t.setCtime(DateUtils.getNowTime());
		t.setCreator(LoginUserCache.getUser().getSysUserName());
		t.setStatus(2);
		if (sysRolesMapper.insert(t) > 0) {
			
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean update(SysRoles t) {
		boolean flag = true;
		t.setUtime(DateUtils.getNowTime());
		t.setModifier(LoginUserCache.getUser().getSysUserName());
		if (sysRolesMapper.updateByPrimaryKey(t) > 0) {
			
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public SysRoles getById(String id) {
		return sysRolesMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<SysRoles> getByMap(Map<String, Object> paramMap) {
		return sysRolesMapper.selectAll(paramMap);
	}

	@Override
	public boolean updateStatusById(String id) {
		boolean flag = true;
		SysRoles sysRoles = getById(id);
		if (sysRoles != null) {
			sysRoles.setModifier(LoginUserCache.getUser().getSysUserName());
			sysRoles.setUtime(DateUtils.getNowTime());
			sysRoles.setStatus(0);
		}
		if (sysRolesMapper.updateByPrimaryKey(sysRoles) > 0) {
			
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean delById(String id) {
		boolean flag = true;
		if (sysRolesMapper.deleteByPrimaryKey(id) > 0) {
			
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public List<SysRoles> selectByName(String name) {
		return sysRolesMapper.selectByName(name);
	}
	
	

}
